﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace IslandInterloper.Engine
{
    internal static class Payload
    {
        internal static string GetString(XElement payload, XName name, out bool success,string defaultValue)
        {
            success = false;
            if (payload == null) return default(string);
            string result = defaultValue;
            if (payload.Element(name) != null)
            {
                result = payload.Element(name).Value;
                success = true;
            }
            return result;
        }
        private static string GetString(XElement payload, XName name, out bool success)
        {
            return GetString(payload, name, out success, default(string));
        }
        internal static bool GetBool(XElement payload, XName name, out bool success, bool defaultValue)
        {
            string theString = GetString(payload, name, out success);
            if (!success) return defaultValue;
            bool result;
            success = bool.TryParse(theString, out result);
            return result;
        }
        internal static int GetInt(XElement payload, XName name, out bool success, int defaultValue)
        {
            string theString = GetString(payload, name, out success);
            if (!success) return defaultValue;
            int result;
            success = int.TryParse(theString, out result);
            return result;
        }
        internal static double GetDouble(XElement payload, XName name, out bool success, double defaultValue)
        {
            string theString = GetString(payload, name, out success);
            if (!success) return defaultValue;
            double result;
            success = double.TryParse(theString, out result);
            return result;
        }
        internal static EnumeratedType GetEnum<EnumeratedType>(XElement payload, XName name, out bool success, EnumeratedType defaultValue)
        {
            EnumeratedType result = defaultValue;
            string theString = GetString(payload, name, out success);
            if (!success) return result;
            try
            {
                result = (EnumeratedType)Enum.Parse(typeof(EnumeratedType),theString);
                success = true;
            }
            catch
            {
                success = false;
            }
            return result;
        }
        internal static ILocation GetLocation(XElement payload, XName name, out bool success)
        {
            success = false;
            if (payload == null) return null;
            XElement element = payload.Element(name);
            if (element == null) return null;
            double x = GetDouble(element, GameXNames.x, out success, 0.0);
            if (!success) return null;
            double y = GetDouble(element, GameXNames.y, out success, 0.0);
            if(!success) return null;
            return new Location(x, y);
        }
    }
}
